package com.lanou3g.demo.controller;

import com.lanou3g.demo.commons.ResultWrapper;
import com.lanou3g.demo.entity.Account;
import com.lanou3g.demo.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private IAccountService service;

    //   查看
    @RequestMapping("/findAll")
    public List<Account> findAll() {
        return service.findAll();
    }

    //  添加
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public Integer addOne(Account account) {
        System.out.println(account);
        account.setStatus("1");
        Integer i = service.addOne(account);
        System.out.println(i);
        return i;
    }

    //    状态更新(未完善)
    @RequestMapping("/upStatus")
    public int upStatus(HttpServletRequest request) {
        String sta = request.getParameter("accountId");
        System.out.println(Integer.valueOf(sta));
        int upStatus = service.upStatus(Integer.valueOf(sta));
        return upStatus;
    }

    //     删除
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Integer deleteById(Account account) {
        Integer accountId = account.getAccountId();
        System.out.println(accountId);
        Integer ii = service.deleteById(accountId);
        return ii;
    }
    //  修改 先查后改
    @RequestMapping(value = "/findById",method = RequestMethod.POST)
    public Account findById(Account account ){
        return  service.findById(account.getAccountId());
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public  int  update(Account account){
        System.out.println("更新前="+account);
        int ii = service.update(account);
        return  ii;
    }
//    搜索
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public  List<Account> search(Account account){
        System.out.println(account.getRealName()+"????");
        List<Account> search = service.search(account);
        System.out.println("********************");
        search.forEach(System.out::println);
        return search;

    }
//    查找某一页
    @RequestMapping(value = "/findPage",method = RequestMethod.GET)
    public ResultWrapper findPage(Integer page,Integer count){
        System.out.println("Controller层::::page = " + page + "   count:" + count);
        if (page <= 0 || count <= 0) {
            System.out.println("aaa");
            return ResultWrapper.error(444, "出错了");
        }
        return service.findPages(page,count);

    }











}
